package com.molichuxing.services.business.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseCodeEnum;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.business.convertor.Convert;
import com.molichuxing.services.business.dto.request.create.AccidentCaseCreateBizDto;
import com.molichuxing.services.business.dto.request.create.AccidentCreateBizDto;
import com.molichuxing.services.business.dto.request.create.AccidentExamineCreateDto;
import com.molichuxing.services.business.dto.request.create.AccidentImageCreateBizDto;
import com.molichuxing.services.business.dto.response.AccidentBizDto;
import com.molichuxing.services.business.dto.response.AccidentCaseAppDto;
import com.molichuxing.services.business.dto.response.AccidentCaseDetailAppDto;
import com.molichuxing.services.business.dto.response.AccidentDetailAppBizDto;
import com.molichuxing.services.business.dto.response.AccidentDetailDto;
import com.molichuxing.services.business.dto.response.AccidentDownloadBizDto;
import com.molichuxing.services.business.dto.response.AccidentInfoDto;
import com.molichuxing.services.business.service.CarAccidentBizService;
import com.molichuxing.services.exception.VehicleExceptionCodeEnum;
import com.molichuxing.services.infrastructure.dto.request.create.AccidentDriveCreateDto;
import com.molichuxing.services.infrastructure.dto.request.create.AccidentImageCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.AccidentCaseModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.AccidentModifyDto;
import com.molichuxing.services.infrastructure.dto.response.AccidentCaseDto;
import com.molichuxing.services.infrastructure.dto.response.AccidentDownloadDto;
import com.molichuxing.services.infrastructure.dto.response.AccidentDriveDto;
import com.molichuxing.services.infrastructure.dto.response.AccidentDto;
import com.molichuxing.services.infrastructure.dto.response.AccidentImageDto;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.dto.response.InsuranceDto;
import com.molichuxing.services.infrastructure.service.AccidentCaseService;
import com.molichuxing.services.infrastructure.service.AccidentDriveService;
import com.molichuxing.services.infrastructure.service.AccidentImageService;
import com.molichuxing.services.infrastructure.service.AccidentService;
import com.molichuxing.services.infrastructure.service.CarService;
import com.molichuxing.services.infrastructure.service.InsuranceService;
import com.molichuxing.services.property.CarAccidentImageEnum;
import com.molichuxing.services.property.CarAccidentLiabilityTypeEnum;
import com.molichuxing.services.property.CarAccidentStatusEnum;

/**
 * <p>
 * 车辆检验列表服务实现类
 * </p>
 * @author xuelin yi
 */
@Service("carAccidentBizService")
public class CarAccidentBizServiceImpl implements CarAccidentBizService {

	private static final Logger logger = LoggerFactory.getLogger(CarAccidentBizServiceImpl.class);

	@Resource
	private AccidentService accidentService;

	@Resource
	private AccidentCaseService accidentCaseService;

	@Resource
	private CarService carService;

	@Resource
    private InsuranceService insuranceService;

	@Resource
    private AccidentDriveService accidentDriveService;

	@Resource
	private AccidentImageService accidentImageService;

	@Override
	public Paged<AccidentBizDto> getCarAccidentPage(Map<String, Object> map, Integer pageNum, Integer pageSize) {
		Paged<AccidentBizDto> paged = new Paged<>();
		paged.setTab(getTabList(map));
		if((null != map.get("licencePlate"))&&(map.get("licencePlate").equals("-1"))) {
			return paged;
		}
		if(null != map.get("licencePlate")||null != map.get("vin")) {
            CarDto carDto = carService.getByVinLicencePlate(ObjectCastUtil.castString(map.get("vin")), ObjectCastUtil.castString(map.get("licencePlate")));
            if(null == carDto) {
				return paged;
			}
            map.put("carId", carDto.getId());
        }
		if(null != map.get("startTime")) {
			map.put("startTime", map.get("startTime")+" 00:00:00");
		}
		if(null != map.get("endTime")) {
			map.put("endTime", map.get("endTime")+" 23:59:59");
		}
		Paged<AccidentDto> resultPage = accidentService.getCarAccidentPage(map, pageNum, pageSize);
		Map<Integer, CarDto> carMap = carService.getCarByIds(getCarIds(resultPage.getList()));
		List<Integer> accidentIds = getAccidentCarIds(resultPage.getList());
		Map<Integer, AccidentCaseDto> accidentCaseMap = null;
		if(null != accidentIds && accidentIds.size() > 0) {
			accidentCaseMap = accidentCaseService.getCarAccidentCaseList(accidentIds);
		}
		paged = Convert.toAccidentBizDto(resultPage, carMap, accidentCaseMap);
		paged.setTab(getTabList(map));
		return paged;
	}

	@Override
	public List<AccidentBizDto> getAccidentByCarId(Integer carId) {
		List<AccidentDto> accidentList = accidentService.getAccidentByCarId(carId);
		Map<Integer, AccidentCaseDto> accidentCaseMap = null;
		List<Integer> accidentIds = getAccidentCarIds(accidentList);
		if(null != accidentIds && accidentIds.size() > 0) {
			accidentCaseMap = accidentCaseService.getCarAccidentCaseList(accidentIds);
		}
		return Convert.toAccidentBizDto(accidentList, null, accidentCaseMap);
	}

	@Override
	public AccidentDetailDto getAccident(Integer accidentId) throws Exception {
		AccidentDetailDto accidentDetailDto = new AccidentDetailDto();
		AccidentInfoDto accidentInfoDto = new AccidentInfoDto();
		AccidentDto accidentDto = accidentService.getCarAccident(accidentId);
        if(null == accidentDto) {
            return null;
        }
        CarDto carDto = carService.getCarById(accidentDto.getCarId());
        if(null == carDto) {
            return null;
        }
        List<InsuranceDto> insuranceList = insuranceService.getInsuranceByCarId(carDto.getId(), null);
		Convert.toUserAccident(accidentDetailDto, accidentDto, carDto, insuranceList);

		BeanUtils.copyProperties(accidentDto, accidentInfoDto);

		AccidentCaseDto accidentCaseDto = accidentCaseService.getCarAccidentCase(accidentId);
		Convert.toCommitAccident(accidentDetailDto, accidentDto, accidentCaseDto);
		if(null != accidentCaseDto) {
			if(null != accidentCaseDto.getIsDie()) {
				accidentInfoDto.setIsDie(accidentCaseDto.getIsDie());
			}
			if(null != accidentCaseDto.getSettleTime()) {
				accidentInfoDto.setCaseTime(accidentCaseDto.getSettleTime());
			}
			if(null != accidentCaseDto.getSettleRemark()) {
				accidentInfoDto.setCaseExplain(accidentCaseDto.getSettleRemark());
			}
			if(null != accidentCaseDto.getVersion()) {
				accidentDetailDto.setCaseVersion(accidentCaseDto.getVersion());
			}
		}
		AccidentDriveDto accidentDriveDto = accidentDriveService.getCarAccidentDrive(accidentId);
		Map<Integer, List<AccidentImageDto>> accidentImageMap = accidentImageService.getCarAccidentImageList(accidentId, null);
		Convert.toAccidentImage(accidentDetailDto, accidentDriveDto, accidentImageMap, accidentDto);

		Convert.toAccidentStatusDto(accidentDetailDto, accidentDto, accidentCaseDto);

		accidentDetailDto.setId(accidentDto.getId());
		accidentDetailDto.setCode(accidentDto.getCode());
		accidentDetailDto.setAccidentVersion(accidentDto.getVersion());
		accidentDetailDto.setAccidentInfoDto(accidentInfoDto);

		return accidentDetailDto;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean modifyExamineAccident(AccidentExamineCreateDto createDto)throws Exception {
		if(createDto.getType() == 1) {
			AccidentDto accidentDto = accidentService.getCarAccident(createDto.getAccidentId());
			if(null == accidentDto) {
				throw new BizException("数据不存在");
			}
			if(!accidentDto.getVersion().equals(createDto.getVersion())) {
				throw new BizException("乐观锁标识标识不一致");
			}
			AccidentModifyDto modifyDto = new AccidentModifyDto();
			modifyDto.setId(createDto.getAccidentId());
			modifyDto.setRemark(createDto.getRemark());
			modifyDto.setAccountId(createDto.getOperateId());
			modifyDto.setVersion(createDto.getVersion()+1);
			modifyDto.setAccountAccount(createDto.getOperateName());
			modifyDto.setOperationTime(LocalDateTime.now());
			if(createDto.getAccidentStatus() == 1) {
				modifyDto.setStatus(CarAccidentStatusEnum.ACCIDENT_CONFIRMED.getValue());
			}else if(createDto.getAccidentStatus() == 2) {
				modifyDto.setStatus(CarAccidentStatusEnum.INVALID_REPORT.getValue());
			}else {
				throw new BizException(VehicleExceptionCodeEnum.NOT_EXIST.getMessage());
			}
			return accidentService.modifyAccident(modifyDto);
		}else if(createDto.getType() == 2) {
			AccidentCaseDto accidentCaseDto = accidentCaseService.getCarAccidentCase(createDto.getAccidentId());
			if(null == accidentCaseDto) {
				throw new BizException("数据不存在");
			}
			if(!accidentCaseDto.getVersion().equals(createDto.getVersion())) {
				throw new BizException("乐观锁标识标识不一致");
			}
			AccidentModifyDto modifyDto = new AccidentModifyDto();
			modifyDto.setId(createDto.getAccidentId());
			modifyDto.setStatus(CarAccidentStatusEnum.CASE_CLOSED.getValue());
			accidentService.modifyAccident(modifyDto);
			AccidentCaseModifyDto caseModifyDto = new AccidentCaseModifyDto();
			caseModifyDto.setId(createDto.getAccidentId());
			caseModifyDto.setVersion(createDto.getVersion()+1);
			caseModifyDto.setExmineRemark(createDto.getRemark());
			caseModifyDto.setAccountId(createDto.getOperateId());
			caseModifyDto.setAccountAccount(createDto.getOperateName());
			caseModifyDto.setOperationTime(LocalDateTime.now());
			return accidentCaseService.modifyAccidentCase(caseModifyDto);
		}
		return null;
	}

	@Override
	public AccidentDetailAppBizDto getAppAccident(Integer accidentId) throws Exception {
		AccidentDetailAppBizDto accidentDetailAppBizDto = new AccidentDetailAppBizDto();
		//基础信息
		AccidentDto accidentDto = accidentService.getCarAccident(accidentId);
		if(null == accidentDto){
			throw new Exception("基础信息不存在");
		}
		accidentDetailAppBizDto.setAccidentAppBizDto(Convert.toAccidentAppBizDto(accidentDto));

		//驾驶人信息
		AccidentDriveDto accidentDriveDto = accidentDriveService.getCarAccidentDrive(accidentId);
		if(null == accidentDto){
			throw new Exception("驾驶人信息不存在");
		}
		accidentDetailAppBizDto.setAccidentDriveAppBizDto(Convert.toAccidentDriveAppBizDto(accidentDriveDto));

		//图片上传信息
		List<CarAccidentImageEnum> carAccidentImageEnum = getCarAccidentImageEnumList(accidentDto);

		Map<Integer, List<AccidentImageDto>> accidentImageMap = accidentImageService.getCarAccidentImageList(accidentId, carAccidentImageEnum);
		accidentDetailAppBizDto.setAccidentImageAppBizDtoList(Convert.toAccidentImageAppBizDto(carAccidentImageEnum,accidentImageMap));

		return accidentDetailAppBizDto;
	}

	@Override
	public AccidentCaseDetailAppDto getAccidentCaseDetailApp(Integer accidentId) throws Exception {
		AccidentCaseDetailAppDto accidentCaseDetailAppDto = new AccidentCaseDetailAppDto();

		//基础信息
		AccidentDto accidentDto = accidentService.getCarAccident(accidentId);
		if(null == accidentDto){
			throw new Exception("基础信息不存在");
		}

		//结案信息
		AccidentCaseDto accidentCaseDto = accidentCaseService.getCarAccidentCase(accidentId);
		if(null == accidentCaseDto){
			throw new Exception("结案信息不存在");
		}
		AccidentCaseAppDto accidentCaseAppDto = Convert.toAccidentCaseAppDto(accidentCaseDto);
		accidentCaseAppDto.setStatus(accidentDto.getStatus());
		accidentCaseDetailAppDto.setAccidentCaseAppDto(accidentCaseAppDto);

		//图片上传信息
		List<CarAccidentImageEnum> carAccidentImageEnum = getCarCaseImageEnumList(accidentDto);

		Map<Integer, List<AccidentImageDto>> accidentImageMap = accidentImageService.getCarAccidentImageList(accidentId, carAccidentImageEnum);
		accidentCaseDetailAppDto.setAccidentImageAppBizDto(Convert.toAccidentImageAppBizDto(carAccidentImageEnum,accidentImageMap));
		return accidentCaseDetailAppDto;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean createAccidentCreateBiz(AccidentCreateBizDto accidentCreateBizDto) throws Exception {
		if(null == accidentCreateBizDto.getAccidentCreateDto().getId()){
			//新增
			//创建事故基础信息
			Integer accidentId = accidentService.createAccident(accidentCreateBizDto.getAccidentCreateDto());
			if(null == accidentId ){
				throw new Exception("创建事故基础信息失败");
			}
			//创建驾驶人信息
			AccidentDriveCreateDto accidentDriveCreateDto = accidentCreateBizDto.getAccidentDriveCreateDto();
			accidentDriveCreateDto.setId(accidentId);
			Boolean  accidentDriveFlag= accidentDriveService.createAccidentDrive(accidentDriveCreateDto);
			if(!accidentDriveFlag){
				throw new Exception("创建驾驶人信息失败");
			}

			if (null != accidentCreateBizDto.getAccidentImageCreateVoList() && accidentCreateBizDto.getAccidentImageCreateVoList().size() > 0) {
				AccidentImageCreateDto accidentImageCreateDto = null;
				for (AccidentImageCreateBizDto accidentImageCreateBizDto : accidentCreateBizDto.getAccidentImageCreateVoList()) {
					if(null != accidentImageCreateBizDto.getImageList() && accidentImageCreateBizDto.getImageList().size() > 0){
						for (String path : accidentImageCreateBizDto.getImageList()) {
							accidentImageCreateDto = new AccidentImageCreateDto();
							accidentImageCreateDto.setAccidentId(accidentId);
							accidentImageCreateDto.setRelativePath(path);
							accidentImageCreateDto.setType(accidentImageCreateBizDto.getType());
							Boolean createImageFlag = accidentImageService.createAccidentImage(accidentImageCreateDto);
							if (!createImageFlag) {
								throw new Exception("更新图片信息失败");
							}
						}
					}
				}
			}
		}else{
			//修改
			Integer accidentId = accidentCreateBizDto.getAccidentCreateDto().getId();

			//更新事故基础信息
			int updateAccidentFlag = accidentService.updateAccidentById(accidentCreateBizDto.getAccidentCreateDto());
			if(updateAccidentFlag < 0){
				throw new Exception("更新事故基础信息失败");
			}
			//创建驾驶人信息
			AccidentDriveCreateDto accidentDriveCreateDto = accidentCreateBizDto.getAccidentDriveCreateDto();
			accidentDriveCreateDto.setId(accidentId);
			int  accidentDriveFlag= accidentDriveService.updateAccidentDriveById(accidentDriveCreateDto);
			if(accidentDriveFlag < 0){
				throw new Exception("更新驾驶人信息失败");
			}
			if(null != accidentCreateBizDto.getAccidentImageCreateVoList() && accidentCreateBizDto.getAccidentImageCreateVoList().size() > 0) {
				AccidentImageCreateDto accidentImageCreateDto = null;
				for (AccidentImageCreateBizDto accidentImageCreateBizDto : accidentCreateBizDto.getAccidentImageCreateVoList()) {
					accidentImageService.deleteAccidentImage(accidentId, accidentImageCreateBizDto.getType());
					if(null != accidentImageCreateBizDto.getImageList() && accidentImageCreateBizDto.getImageList().size() > 0){
						for (String path : accidentImageCreateBizDto.getImageList()) {
							accidentImageCreateDto = new AccidentImageCreateDto();
							accidentImageCreateDto.setAccidentId(accidentId);
							accidentImageCreateDto.setRelativePath(path);
							accidentImageCreateDto.setType(accidentImageCreateBizDto.getType());
							Boolean createImageFlag = accidentImageService.createAccidentImage(accidentImageCreateDto);
							if (!createImageFlag) {
								throw new Exception("更新图片信息失败");
							}
						}
					}
				}
			}
		}

		return true;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean createAccidentCaseCreateBiz(AccidentCaseCreateBizDto accidentCaseCreateBizDto) throws Exception,BizException {
		Integer accidentId = accidentCaseCreateBizDto.getAccidentCaseCreateDto().getId();
		AccidentCaseDto accidentCaseDto = accidentCaseService.getCarAccidentCase(accidentId);
		if(null == accidentCaseDto ){
			//新增
			Boolean createAccidentCaseFlag = accidentCaseService.createAccidentCase(accidentCaseCreateBizDto.getAccidentCaseCreateDto());
			if(!createAccidentCaseFlag){
				throw new Exception("创建结案信息失败");
			}
			if (null != accidentCaseCreateBizDto.getAccidentImageCreateVoList() && accidentCaseCreateBizDto.getAccidentImageCreateVoList().size() > 0) {
				AccidentImageCreateDto accidentImageCreateDto = null;
				for (AccidentImageCreateBizDto accidentImageCreateBizDto : accidentCaseCreateBizDto.getAccidentImageCreateVoList()) {
					if(null != accidentImageCreateBizDto.getImageList() && accidentImageCreateBizDto.getImageList().size() > 0){
						for (String path : accidentImageCreateBizDto.getImageList()) {
							accidentImageCreateDto = new AccidentImageCreateDto();
							accidentImageCreateDto.setAccidentId(accidentId);
							accidentImageCreateDto.setRelativePath(path);
							accidentImageCreateDto.setType(accidentImageCreateBizDto.getType());
							Boolean createImageFlag = accidentImageService.createAccidentImage(accidentImageCreateDto);
							if (!createImageFlag) {
								throw new Exception("更新图片信息失败");
							}
						}
					}
				}
			}
			//维护事故主状态  改为：3结案未确认
			accidentService.updateAccidentStatusById(accidentId,CarAccidentStatusEnum.CASE_NOT_CONFIRMED.getValue());
		}else{
		    if(!accidentCaseDto.getVersion().equals(accidentCaseCreateBizDto.getAccidentCaseCreateDto().getVersion())){
				throw new BizException(ResponseCodeEnum.ABNORMAL_ERROR.getCode(), "状态已发生变化，无法提交");
            }
			//修改
			int createAccidentCaseFlag = accidentCaseService.updateAccidentCaseById(accidentCaseCreateBizDto.getAccidentCaseCreateDto());
			if(createAccidentCaseFlag < 0){
				throw new Exception("更新结案信息失败");
			}

			if(null != accidentCaseCreateBizDto.getAccidentImageCreateVoList() && accidentCaseCreateBizDto.getAccidentImageCreateVoList().size() > 0) {
				AccidentImageCreateDto accidentImageCreateDto = null;
				for (AccidentImageCreateBizDto accidentImageCreateBizDto : accidentCaseCreateBizDto.getAccidentImageCreateVoList()) {
					accidentImageService.deleteAccidentImage(accidentId, accidentImageCreateBizDto.getType());
					if(null != accidentImageCreateBizDto.getImageList() && accidentImageCreateBizDto.getImageList().size() > 0){
						for (String path : accidentImageCreateBizDto.getImageList()) {
							accidentImageCreateDto = new AccidentImageCreateDto();
							accidentImageCreateDto.setAccidentId(accidentId);
							accidentImageCreateDto.setRelativePath(path);
							accidentImageCreateDto.setType(accidentImageCreateBizDto.getType());
							Boolean createImageFlag = accidentImageService.createAccidentImage(accidentImageCreateDto);
							if (!createImageFlag) {
								throw new Exception("更新图片信息失败");
							}
						}
					}
				}
			}
		}

		return true;
	}


	/**
	 * app端 结案详情-图片上传信息所需枚举
	 * @param accidentDto
	 * @return
	 */
	private static List<CarAccidentImageEnum> getCarCaseImageEnumList(AccidentDto accidentDto) {
		List<CarAccidentImageEnum> carAccidentImageEnumList= new ArrayList<>();
		carAccidentImageEnumList.add(CarAccidentImageEnum.ACCIDENT_REPAIR_STATEMENT);
		carAccidentImageEnumList.add(CarAccidentImageEnum.MAINTENANCE_INVOICE);
		carAccidentImageEnumList.add(CarAccidentImageEnum.PHOTOGRAPHS_AFTER_VEHICLE_REPAIR);
		//（0：无责，other：有责）
		if(CarAccidentLiabilityTypeEnum.NO_RESPONSIBILITY.getValue() == accidentDto.getLiabilityType()){
			//（0false：无人伤，1true：有人伤）
			if(accidentDto.getIsCasualty()){
				carAccidentImageEnumList.add(CarAccidentImageEnum.POSITIVE_PHOTO_BANK_CARD);
				carAccidentImageEnumList.add(CarAccidentImageEnum.CERTIFICATE_MEDICAL_EXPENSES);
				carAccidentImageEnumList.add(CarAccidentImageEnum.TRAFFIC_COST);
				carAccidentImageEnumList.add(CarAccidentImageEnum.PROOF_OF_PAYMENT);
				carAccidentImageEnumList.add(CarAccidentImageEnum.CERTIFICATION_COMPENSATION);
				carAccidentImageEnumList.add(CarAccidentImageEnum.CHARGE_LOSS_TIME);
				carAccidentImageEnumList.add(CarAccidentImageEnum.TREATMENT_AFTER_DISCHARGE);
				carAccidentImageEnumList.add(CarAccidentImageEnum.DEATH_CERTIFICATE);
				carAccidentImageEnumList.add(CarAccidentImageEnum.DEATH_CERTIFICATE_COST);
			}
		}else{
			carAccidentImageEnumList.add(CarAccidentImageEnum.DAMAGE_ORDER);
			//（0false：无人伤，1true：有人伤）
			if(accidentDto.getIsCasualty()){
				carAccidentImageEnumList.add(CarAccidentImageEnum.POSITIVE_PHOTO_BANK_CARD);
				carAccidentImageEnumList.add(CarAccidentImageEnum.CERTIFICATE_MEDICAL_EXPENSES);
				carAccidentImageEnumList.add(CarAccidentImageEnum.TRAFFIC_COST);
				carAccidentImageEnumList.add(CarAccidentImageEnum.PROOF_OF_PAYMENT);
				carAccidentImageEnumList.add(CarAccidentImageEnum.CERTIFICATION_COMPENSATION);
				carAccidentImageEnumList.add(CarAccidentImageEnum.CHARGE_LOSS_TIME);
				carAccidentImageEnumList.add(CarAccidentImageEnum.TREATMENT_AFTER_DISCHARGE);
				carAccidentImageEnumList.add(CarAccidentImageEnum.DEATH_CERTIFICATE);
				carAccidentImageEnumList.add(CarAccidentImageEnum.DEATH_CERTIFICATE_COST);

			}else{
				carAccidentImageEnumList.add(CarAccidentImageEnum.POSITIVE_PHOTO_OF_MY_BANK_CARD);

			}

		}
		return carAccidentImageEnumList;
	}


	/**
	 * app端 事故详情-图片上传信息所需枚举
	 * @param accidentDto
	 * @return
	 */
	private static List<CarAccidentImageEnum> getCarAccidentImageEnumList(AccidentDto accidentDto) {
		List<CarAccidentImageEnum> carAccidentImageEnumList= new ArrayList<>();
		carAccidentImageEnumList.add(CarAccidentImageEnum.VEHICLE_DEGREE_ANGLE);
		carAccidentImageEnumList.add(CarAccidentImageEnum.ACCIDENT_PANORAMA);
		carAccidentImageEnumList.add(CarAccidentImageEnum.COLLISION_DETAILS);
		carAccidentImageEnumList.add(CarAccidentImageEnum.SCATTERED_PARTS);
		carAccidentImageEnumList.add(CarAccidentImageEnum.FRAME_NUMBER_PHOTO);
		carAccidentImageEnumList.add(CarAccidentImageEnum.INTERIOR_PHOTO);
		//（0false：无人伤，1true：有人伤）
		if(accidentDto.getIsCasualty()){
			carAccidentImageEnumList.add(CarAccidentImageEnum.PANORAMA_WOUNDED);
			carAccidentImageEnumList.add(CarAccidentImageEnum.DETAILS_INJURED_AREA);
		}
		return carAccidentImageEnumList;
	}


	private List<Integer> getCarIds(List<AccidentDto> accidentDtoList) {
		List<Integer> result = null;
		if((null != accidentDtoList)&&(accidentDtoList.size() > 0)) {
			result = new ArrayList<>();
			for(AccidentDto accidentDto : accidentDtoList) {
				result.add(accidentDto.getCarId());
			}
		}
		return result;
	}

	private static List<Integer> getAccidentCarIds(List<AccidentDto> accidentDtoList) {
		List<Integer> result = null;
		if((null != accidentDtoList)&&(accidentDtoList.size() > 0)) {
			result = new ArrayList<>();
			for(AccidentDto accidentDto : accidentDtoList) {
				result.add(accidentDto.getId());
			}
		}
		return result;
	}


	private List<TabDto> getTabList(Map<String, Object> paramMap) {
		Map<String, Object> map = new HashMap<>();
		List<TabDto> tabList = new ArrayList<>();
		for(CarAccidentStatusEnum statusEnum : CarAccidentStatusEnum.values()) {
			/*if(CarAccidentStatusEnum.ALL != statusEnum) {
				map.put("status", statusEnum.getValue());
			}*/
			map.put("status", statusEnum.getValue());
			if(null != paramMap.get("dealerId")) {
				map.put("dealerId", paramMap.get("dealerId"));
			}
			int count = accidentService.getCarAccidentTabCount(map);
			TabDto tabDto = new TabDto();
			tabDto.setCount(count);
			tabDto.setName(statusEnum.getName());
			tabDto.setType(statusEnum.getValue());
			tabList.add(tabDto);
		}
		return tabList;
	}
	
	 /**
	   * 查询事故报备导出数据
	   *    * ids id集合
	   *    * licencePlate 车牌号
	   *    * vin 车架号
	   *    * userId 用户id
	   *    * liabilityType 责任类型（0：无责，1：全责，2：主责，3：同责，4：次责）
	   *    * isCasualty 是否有人伤（0：无人伤，1：有人伤）
	   *    * accidentType 事故类型（1:单方事故，2：双方事故，3：多方事故，4：停放受损，5：车辆盗抢，6：车辆自燃，7：车辆涉水，8：重大自然灾害）
	   *    * accidentStartTime     出险开始时间
	   *    * accidentEndTime     出险结束时间
	   *    * status 事故状态（1：事故未确认，2：事故已确认，3：结案未确认，4：已结案，5：无效报备）
	   * @param map
	   * @return
	   */
	@Override
	public List<AccidentDownloadBizDto> getAccidentDownload(Map<String, Object> map) {
		if(null != map.get("licencePlate")||null != map.get("vin")) {
            CarDto carDto = carService.getByVinLicencePlate(ObjectCastUtil.castString(map.get("vin")), ObjectCastUtil.castString(map.get("licencePlate")));
            if(null == carDto) {
				return null;
			}
            map.put("carId", carDto.getId());
        }
		if(null != map.get("startTime")) {
			map.put("startTime", map.get("startTime")+" 00:00:00");
		}
		if(null != map.get("endTime")) {
			map.put("endTime", map.get("endTime")+" 23:59:59");
		}
		List<AccidentDownloadDto> list = accidentService.getAccidentDownload(map);
		if(null == list || list.isEmpty()) {
			return null;
		}
		List<Integer> carIds =new ArrayList<>();
		for (AccidentDownloadDto dto : list) {
			carIds.add(dto.getCarId());
		}
		Map<Integer, CarDto> carMap = carService.getCarByIds(carIds);
		List<AccidentDownloadBizDto> bizDtos = new ArrayList<>(list.size());
		for (AccidentDownloadDto dto : list) {
			AccidentDownloadBizDto bizDto = new AccidentDownloadBizDto();
			BeanUtils.copyProperties(dto, bizDto);
			
			//获取车辆信息
			CarDto carDto= null == carMap ? null : carMap.get(dto.getCarId());
			bizDto.setVin(carDto == null ? null : carDto.getVin());
			bizDto.setLicencePlate(carDto == null ? null : carDto.getLicencePlate());
			bizDtos.add(bizDto);
		}
		return bizDtos;
	}

	/**
	 * 查询事故报备导出数据条数
	 * 
	 * @param selectParams
	 * @return
	 */
	@Override
	public Long getAccidentDownloadCount(Map<String, Object> selectParams) {
		if (null != selectParams.get("licencePlate") || null != selectParams.get("vin")) {
			CarDto carDto = carService.getByVinLicencePlate(ObjectCastUtil.castString(selectParams.get("vin")),
					ObjectCastUtil.castString(selectParams.get("licencePlate")));
			if (null == carDto) {
				return null;
			}
			selectParams.put("carId", carDto.getId());
		}
		if (null != selectParams.get("startTime")) {
			selectParams.put("startTime", selectParams.get("startTime") + " 00:00:00");
		}
		if (null != selectParams.get("endTime")) {
			selectParams.put("endTime", selectParams.get("endTime") + " 23:59:59");
		}
		return accidentService.getAccidentDownloadCount(selectParams);
	}

}
